<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'

	const userManage = uniCloud.importObject('user-manage')
	const openid = ref(getApp().globalData.openid)
	const userInfoData = ref(null)

	onLoad(() => {
		const storageData = uni.getStorageSync('userInfo');
		if (storageData) {
			const data = JSON.parse(storageData)
			userInfoData.value = data
			console.log(userInfoData.value, '!!!!!!');
			nickname.value = data.name
			inputNickname.value = data.name
			headImageURL.value = data.avatarURL
		} else {
			uni.showToast({
				title: '网络错误，请联系开发者或稍后重试',
				icon: 'error'
			})
		}
	})

	const navbarOptions = {
		fontSize: 20,
		iconfont: 'icon-back'
	}
	const nickname = ref('')
	const inputNickname = ref('')

	function submitNickname() {
		const currentNicname = inputNickname.value.trim()
		if (currentNicname === '') {
			uni.showToast({
				title: '输入的昵称不能为空!',
				icon: 'none'
			})
			inputNickname.value = nickname.value
			return
		}
		if (currentNicname === nickname.value) {
			uni.showToast({
				title: '哇哦，你真的很喜欢这个名字呢，两次都是一样的!',
				icon: 'none'
			})
			inputNickname.value = nickname.value
			return
		}
		userManage.updateUserInfo({
			userID: openid.value,
			name: currentNicname
		})
		userInfoData.value = {
			...userInfoData.value,
			name: currentNicname
		}
		uni.setStorageSync('userInfo', JSON.stringify(userInfoData.value))
		nickname.value = currentNicname
	}

	const headImageURL = ref('')
	const isDarkMode = ref(false)

	// 切换黑暗模式
	function changeDarkMode(e) {
		isDarkMode.value = e.detail.value
	}

	// 修改头像
	async function changeHeadImage({
		detail: {
			avatarUrl
		}
	}) {
		const {
			fileID
		} = await uniCloud.uploadFile({
			cloudPath: '/userAvatar/' + openid.value,
			filePath: avatarUrl,
		})
		userManage.updateUserInfo({
			userID: openid.value,
			avatarID: fileID
		})
		userInfoData.value = {
			...userInfoData.value,
			avatarID: fileID,
			avatarURL: avatarUrl,
		}
		uni.setStorageSync('userInfo', JSON.stringify(userInfoData.value))
		headImageURL.value = avatarUrl
	}
</script>

<template>
	<navbar v-bind="navbarOptions"></navbar>
	<view class="personal-center-page">

		<button class="head-image-box" open-type="chooseAvatar" @chooseavatar="changeHeadImage">
			<image class="image-circle" :src="headImageURL" mode="aspectFill"></image>
		</button>

		<text class="personal-name">{{nickname}}</text>

		<view class="coins-and-history">
			<view class="coins-box">
				<text class="number">7</text>
				<view class="gold-conins">
					<text>金币</text>
					<text class="iconfont icon-wenhao"></text>
				</view>
			</view>
			<view class="vertical-line"></view>
			<view class="history-box">
				<text class="number">7</text>
				<view class="history-number">
					<text>历史场次</text>
					<text class="iconfont icon-forward" style="font-size: 18rpx;"></text>
				</view>
			</view>
		</view>

		<view class="user-info">
			<view class="display-flex uniform-style">
				<text class="iconfont icon-shangchuantouxiang font21"></text>
				<text>头像</text>
				<view class="user-info-head-image">
					<button class="head-image" open-type="chooseAvatar" @chooseavatar="changeHeadImage">
						<image class="image-circle" :src="headImageURL" mode="aspectFill"></image>
					</button>
				</view>
				<text class="iconfont icon-forward font10"></text>
			</view>
			<view class="display-flex uniform-style">
				<text class="iconfont icon-gerenzhongxin font21"></text>
				<text>昵称</text>
				<input class="nickname-input" type="nickname" placeholder="请输入昵称" maxlength="20" v-model="inputNickname"
					@blur="submitNickname" />
				<text class="iconfont icon-forward font10"></text>
			</view>
		</view>

		<view class="other-items">
			<view class="display-flex uniform-style">
				<text class="iconfont icon-jinbi font21"></text>
				<text>金币任务</text>
				<text style="margin-left: auto;" class="iconfont icon-forward font10"></text>
			</view>
			<view class="display-flex uniform-style">
				<text class="iconfont icon-guanyuwomen font21"></text>
				<text>关于我们</text>
				<text style="margin-left: auto;" class="iconfont icon-forward font10"></text>
			</view>
			<view class="display-flex uniform-style">
				<text class="iconfont icon-declare font21"></text>
				<text>免责声明</text>
				<text style="margin-left: auto;" class="iconfont icon-forward font10"></text>
			</view>
			<!-- <view class="display-flex uniform-style">
				<text class="iconfont icon-yansepan font21"></text>
				<text>暗黑模式</text>
				<view class="switch-container">
					<switch color='#6265f2' class="custom-switch" checked="{{isDarkMode}}" @change="changeDarkMode"></switch>
				</view>
			</view> -->
		</view>

	</view>
</template>

<style lang="scss">
	.switch-container {
		margin-left: auto;
		width: 90rpx;
		height: 60rpx;
		line-height: 60rpx;
	}

	.custom-switch {
		transform: scale(.7);
	}

	.font10 {
		margin-left: 20rpx;
		font-size: 20rpx;
		color: var(--theme-color);
	}

	.font21 {
		margin-right: 20rpx;
		font-size: 42rpx;
		color: var(--theme-color);
	}

	.personal-center-page {
		display: flex;
		flex-direction: column;
		align-items: center;

		.head-image-box {
			width: 140rpx;
			height: 140rpx;
			margin-bottom: 20rpx;
			padding: 0;
			border-radius: 50%;
			line-height: 1;
			filter: drop-shadow(4rpx 6rpx 4rpx rgba(0, 0, 0, 0.3));
		}

		.personal-name {
			width: 320rpx;
			height: 64rpx;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
			text-overflow: ellipsis;
			text-align: center;
			line-height: 1.3;
			font-size: 26rpx;
			font-weight: 600;
		}

		.vertical-line {
			width: 2rpx;
			background-color: rgb(202, 202, 202);
			height: 40rpx;
		}

		.coins-and-history {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 420rpx;
			margin: 50rpx 0 20rpx;

			.coins-box,
			.history-box {
				display: flex;
				flex-direction: column;
				align-items: center;

				.number {
					padding-bottom: 10rpx;
					font-size: 30rpx;
					font-weight: bold;
				}
			}

			.gold-conins,
			.history-number {
				&>text {
					padding: 2rpx;
					font-size: 22rpx;
					font-weight: 300;
				}
			}
		}

		.uniform-style {
			padding: 30rpx;
		}

		.user-info,
		.other-items {
			width: calc(100vw - 80rpx);
			margin: 40rpx 80rpx 0;
			border-radius: 20rpx;
			background-color: #ffffff;

			.user-info-head-image {
				flex: 1;
				display: flex;
				justify-content: flex-end;
			}

			.head-image {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				box-shadow: 0, 4rpx, 4rpx, rgba(0, 0, 0, .3);
				background-color: #eeeeee;
			}

			.nickname-input {
				margin-left: auto;
				text-align: right;
			}
		}
	}
</style>